$(function(){
    $("#listen-url-request").click(function () {
        chrome.tabs.query(
            {'active': true, 'lastFocusedWindow': true},
            function(tabsData) {
                for (let activeTab of tabsData) {
                    alert("开始中--------")
                    chrome.runtime.sendMessage({cmd:'listen', value:{},"activeTab":activeTab},function(response) {
                            console.log('content get response:',response);
                        }
                    )
                }
            }
        );
    });
    $("#send-msg").click(function () {
        //桌面通知
        chrome.notifications.create(null, {
            type: 'basic',
            iconUrl: 'img/icon.png',
            title: '测试通知',
            message: '您刚才点击了发送测试通知消息！'
        });
    });
    $('#open-some-url-get-html').click(function(){
        let url = $.trim($("#url").val());
        if(url===""){
            url = 'https://www.baidu.com/s?ie=utf-8&wd=php';
        }
        chrome.tabs.query({'active':true,'windowId':chrome.windows.WINDOW_ID_CURRENT},function(tabsData)
        {
            for (let activeTab of tabsData) {
                chrome.tabs.create({"url": url,"active": false},function (openTab) {
                    console.log(openTab);
                    let waitTime = 5000;
                    alert("等待"+(waitTime/1000)+"秒后开始获取数据");
                    window.setInterval(function () {
                                chrome.tabs.sendMessage(openTab.id,  {cmd:'getHtml', value:'获取当前页面html代码'}, function(response){
                                    chrome.tabs.sendMessage(
                                        activeTab.id,
                                        {cmd:'log', value:response},
                                        function(responseMsg){
                                            alert(responseMsg);
                                            chrome.tabs.remove(openTab.id,()=>{});
                                        }
                                    );
                                });
                    },
                        waitTime
                    );

                });
            }
        });
    });
    $('#get—cookie').click(function(){
        //快速获取当前活动窗口
        // chrome.windows.getCurrent(function(currentWindow)
        // {
        //     console.log(currentWindow);
        //     alert('当前窗口ID：' + currentWindow.id);
        // });


        //详情获取当前活动窗口
        chrome.tabs.query({'active':true,'windowId':chrome.windows.WINDOW_ID_CURRENT},
            function(tabs){
                console.log(chrome);
                console.log(tabs);
                const url = new URL(tabs[0].url);
                chrome.cookies.getAll({
                    domain: url.host
                }, (cookies) => {
                    let currentUrlCookie= "";
                    cookies.map((c) => {
                        currentUrlCookie += "名称：" + c.name + "、值："+c.value+"\n\t";
                    })
                    console.log(currentUrlCookie);
                })
            }
        )
    });
    
    $('#get-local-cache').click(function(){
        chrome.tabs.query(
            {'active': true, 'lastFocusedWindow': true},
            function(tabs) {
                chrome.tabs.sendMessage(
                    tabs[0].id,
                    {cmd:'getAllStorageData', value:'获取所有缓存数据信息'},
                    function(responseOne){
                        chrome.tabs.sendMessage(
                            tabs[0].id,
                            {cmd:'log', value:responseOne},
                            function(response){
                                alert(response);
                            })
                    })
                ;
            }
        );
    });
    
    $('#get-active—index—html').click(function(){
        chrome.tabs.query(
            {'active': true, 'lastFocusedWindow': true},
            function(tabs) {
                chrome.tabs.sendMessage(
                    tabs[0].id,
                    {cmd:'getHtml', value:'获取当前页面html代码'},
                    function(responseOne){
                        chrome.tabs.sendMessage(
                            tabs[0].id,
                            {cmd:'log', value:responseOne},
                            function(response){
                                alert(response);
                            })
                    })
                ;
            }
        );
    });
    
    $('#active—index-screenshot').click(function(){
        chrome.tabs.query(
            {'active': true, 'lastFocusedWindow': true},
            function(tabs) {
                chrome.tabs.sendMessage(
                    tabs[0].id,
                    {cmd:'screenshot', value:'截屏'},
                    function(response){
                        alert(response);
                    })
                ;
            }
        );
    });
    
    $('#close-active—index-left').click(function(){
        // chrome.windows.getCurrent(function(currentWindow)
        // {
        //     chrome.tabs.create({url: 'https://www.baidu.com/s?ie=utf-8&wd=php'});
        // });
        chrome.tabs.query({}, function (tabs) {
            console.log(tabs);
            let activeIndex= 0;
            for (let tab of tabs) {
                activeIndex++;
                if(tab.active){
                    break
                }else {
                    chrome.tabs.remove(tab.id,()=>{});
                }
            }
            console.log("当前活跃窗口索引："+activeIndex);
        });
    });

    $('#close-active—index').click(function(){
        chrome.tabs.query(
            {'active': true, 'lastFocusedWindow': true},
            function(tabs) {
                for (let tab of tabs) {
                    if(tab.active){
                        chrome.tabs.remove(tab.id,()=>{});
                    }
                }
            }
        );
    });

    $('#close-active—index-right').click(function(){
        chrome.tabs.query({}, function (tabs) {
            tabs.reverse();
            console.log(tabs);
            let activeIndex= tabs.length;
            for (let tab of tabs) {
                activeIndex--;
                if(tab.active){
                    break
                }else {
                    chrome.tabs.remove(tab.id,()=>{});
                }
            }
            console.log("当前活跃窗口索引："+activeIndex);
        });
    });
});